package com.fmy.jurisdiction.config.security.service;

import com.fmy.jurisdiction.modular.model.AdminUser;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * @author Mr.fmy
 * @version 1.0.0
 * @ClassName UserDetailsEntity
 * @Description TODO UserDetails实现类
 * @createTime 2019/9/16 17:36
 */
public class UserDetailsEntity extends AdminUser implements UserDetails {

    @Setter
    private Set<String> permissions;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities = new ArrayList<>();
        if (permissions != null) {
            permissions.forEach(item -> {
                authorities.add(new SimpleGrantedAuthority(item));
            });
        }
        return authorities;
    }

    @Override
    public String getPassword() {
        return super.getPassword();
    }

    @Override
    public String getUsername() {
        return super.getUserLogin();
    }

    /**
     * 账号是否过期
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 账号是否锁定
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 凭证是否过期
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 是否启用
     */
    @Override
    public boolean isEnabled() {
        return super.getStatus();
    }
}
